//显示全部
$('.showMore').click(function(e){
	var id = $(this).parents('.user-gwc').attr('id'); 
	$("."+id).show();
	$(this).remove();
});
//全选/反选
$('.checkAll').click(function(e){
	var isChecked = this.checked;
	var date = $(this).parents('.user-gwc').attr('id'); 
// 	console.log(isChecked);
	$("input[class='"+date+"_checkbox checkbox']").each(function(){
		var id = $(this).parents('.cart_item').attr('id'); 
		var item = list[id];
		var price = $("#number_"+id).val()*item['price'];
		console.log(item['food_name']+":"+price+"元");
		if(isChecked && !this.checked){
			totalPrice += price;
		}
		if(!isChecked && this.checked){
			totalPrice -= price;
		}
		this.checked = isChecked;
	}); 
	$("#totalPrice").html(returnFloat(totalPrice));
// 	console.log("总价:"+totalPrice+"元");
});
//单选
$('.checkbox').change(function(e){
	var id = $(this).parents('.cart_item').attr('id'); 
	var item = list[id];
	var price = $("#number_"+id).val()*item['price'];
	if(this.checked){
		totalPrice += price;
	}else{
		totalPrice -= price;
	}
	$("#totalPrice").html(returnFloat(totalPrice));
// 	console.log("总价:"+totalPrice+"元");
});
//数量修改
$('input[type=number]').change(function(e){
	var id = $(this).parents('.cart_item').attr('id'); 
	var num = $(this).val();
	changeNumber(id,num);
});
//显示加减按钮
$('.con-p03').mouseover(function(e){
	var id = $(this).parents('.cart_item').attr('id'); 
	$('#cart_item_j_'+id).show();
	$('#cart_item_x_'+id).show();
})
//隐藏加减按钮
.mouseout(function(e){
	var id = $(this).parents('.cart_item').attr('id'); 
	$('#cart_item_j_'+id).hide();
	$('#cart_item_x_'+id).hide();
});
//添加菜品
$(".btn-add").click(function(e){
	var id = $(this).parents('.cart_item').attr('id'); 
	var num = parseInt($("#number_"+id).val())+1;
	if(num > 99){
		return;
	}
	$("#number_"+id).val(num);
	changeNumber(id,num);
});
//减少菜品
$(".btn-minus").click(function(e){
	var id = $(this).parents('.cart_item').attr('id'); 
	var num = parseInt($("#number_"+id).val())-1;
	if(num < 1){
		return;
	}
	$("#number_"+id).val(num);
	changeNumber(id,num);
});
//菜品数量修改
function changeNumber(id,num){
	var item = list[id];
	var price = (num - item["num"])*item['price'];
	list[id]['num'] = num;
	var isCheck = document.getElementById("checkbox_"+id).checked;
	if(isCheck){
		totalPrice += price;
	}
	$("#cart_item_totalprice_"+id).html(returnFloat(num*item['price']));
	$("#totalPrice").html(returnFloat(totalPrice));
// 	console.log("总价:"+totalPrice+"元");
}
//保留两位小数点,不够两位则补零
function returnFloat(value) {
    value = Math.round(parseFloat(value) * 100) / 100;
    var xsd = value.toString().split(".");
    //Ext.log(xsd.length);
    if(xsd.length==1){
        value = value.toString()+".00";
        return value;
    }
    if(xsd.length>1){
        if(xsd[1].length<2){
            value = value.toString()+"0";
        }
        return value;
    }
}
var id;
$('.btn-delete').click(function (e) {
	e.preventDefault();
	id = $(this).parents('.cart_item').attr('id'); 
	$("#myDeleteModal").modal('show');
});
$('#myDeleteModal').on('click','button.btn-danger',function (a){
	$.post(deleteUrl,{id:id},function(data){
		if(data){
			alert("删除成功");
			$("#food_num").html($("#food_num").html()-1);
			$("#cart_item_"+id).remove();
		}else{
			alert("删除失败");
		}
	});
	$("#myDeleteModal").modal('hide');
});
//结算
$(".balance").click(function(e){
	e.preventDefault();
	if(ptime == 0){
		alert("暂时不提供订购");
		return;
	}
	var checknum = 0;
	$("#order_food").html("");
	$(".checkbox").each(function(){
// 		console.log('checkbox'+this.checked);
		if(this.checked){
			checknum++;
			var id = $(this).parents('.cart_item').attr('id'); 
			var num = $("#number_"+id).val();
			var cid = $(this).val();
			$("#order_food").append('<input type="hidden" name="cart[]" value="'+id+'"/><input type="hidden" name="category[]" value="'+cid+'"/><input type="hidden" name="num[]" value="'+num+'"/>');
		}
	}); 
	if(checknum){
		$("#myOrderModal").modal('show');
	}else{
		alert("请至少先选择一个商品");
	}
});
$('#myOrderModal').on('click','button.btn-danger',function (a){
	$("#form_order").submit();
});
